package com.photoeditor.demo.model.filedownload;

import android.text.TextUtils;

import com.liulishuo.filedownloader.BaseDownloadTask;
import com.liulishuo.filedownloader.FileDownloadListener;

/**
 * Created by edward on 17-11-14.
 */

public abstract class DownloadListener extends FileDownloadListener {
    private String mUrl;
    private String mDownloadPathName;

    public DownloadListener(String url, String downloadPathName) {
        mUrl = url;
        mDownloadPathName = downloadPathName;
    }

    /**
     * 是否和下载任务对应
     * @param task
     * @return
     */
    public boolean match(BaseDownloadTask task) {
        return TextUtils.equals(task.getUrl(), mUrl) && TextUtils.equals(task.getPath(), mDownloadPathName);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof DownloadListener)) {
            return false;
        }
        DownloadListener that = (DownloadListener) obj;
        if (that == this) {
            return true;
        }
        return TextUtils.equals(that.getUrl(), mUrl) && TextUtils.equals(that.getDownloadPathName(), mDownloadPathName);
    }

    public String getDownloadPathName() {
        return mDownloadPathName;
    }

    public String getUrl() {
        return mUrl;
    }

    /**
     * Enqueue, and pending, waiting for {@link #started(BaseDownloadTask)}.
     *
     * @param task       The task
     * @param soFarBytes Already downloaded bytes stored in the db
     * @param totalBytes Total bytes stored in the db
     */
    public void pending(final BaseDownloadTask task, final int soFarBytes,
                        final int totalBytes) {
    }

    /**
     * Finish pending, and start the download runnable.
     *
     * @param task Current task.
     */
    public void started(final BaseDownloadTask task) {
    }

    /**
     * Already connected to the server, and received the Http-response.
     *
     * @param task       The task
     * @param etag       ETag
     * @param isContinue Is resume from breakpoint
     * @param soFarBytes Number of bytes download so far
     * @param totalBytes Total size of the download in bytes
     */
    public void connected(final BaseDownloadTask task, final String etag,
                          final boolean isContinue, final int soFarBytes, final int totalBytes) {
    }

    /**
     * Fetching datum from network and Writing to the local disk.
     *
     * @param task       The task
     * @param soFarBytes Number of bytes download so far
     * @param totalBytes Total size of the download in bytes
     */
    public abstract void progress(final BaseDownloadTask task, final int soFarBytes,
                                  final int totalBytes);

    /**
     * Unlike other methods in {@link #FileDownloadListener}, BlockComplete is executed in other
     * thread than main as default, when you receive this execution, it means has already completed
     * downloading, but just block the execution of {@link #completed(BaseDownloadTask)}. therefore,
     * you can unzip or do some ending operation before {@link #completed(BaseDownloadTask)} in other
     * thread.
     *
     * @param task the current task
     */
    public void blockComplete(final BaseDownloadTask task) {
    }

    /**
     * Occur a exception and has chance{@link BaseDownloadTask#setAutoRetryTimes(int)} to retry and
     * start Retry.
     *
     * @param task          The task
     * @param ex            Why retry
     * @param retryingTimes How many times will retry
     * @param soFarBytes    Number of bytes download so far
     */
    public void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes,
                      final int soFarBytes) {
    }

    // ======================= The task is over, if execute below methods =======================

    /**
     * Achieve complete ceremony.
     * <p/>
     * Complete downloading.
     *
     * @param task The task
     * @see #blockComplete(BaseDownloadTask)
     */
    public abstract void completed(final BaseDownloadTask task);

    /**
     * Task is paused, the vast majority of cases is invoking the {@link BaseDownloadTask#pause()}
     * manually.
     *
     * @param task       The task
     * @param soFarBytes Number of bytes download so far
     * @param totalBytes Total size of the download in bytes
     */
    public void paused(final BaseDownloadTask task, final int soFarBytes,
                       final int totalBytes) {

    }

    /**
     * Occur a exception, but don't has any chance to retry.
     *
     * @param task The task
     * @param e    Any throwable on download pipeline
     * @see com.liulishuo.filedownloader.exception.FileDownloadHttpException
     * @see com.liulishuo.filedownloader.exception.FileDownloadGiveUpRetryException
     * @see com.liulishuo.filedownloader.exception.FileDownloadOutOfSpaceException
     */
    public void error(final BaseDownloadTask task, final Throwable e) {

    }

    /**
     * There has already had some same Tasks(Same-URL & Same-SavePath) in Pending-Queue or is
     * running.
     *
     * @param task The task
     */
    public void warn(final BaseDownloadTask task) {

    }

}
